/*
 * This file is part of Smart Route for S60 3rd Edition terminals
 * 
 * Copyright (C) 2006  Harri Salminen

 * This program free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 *(at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
 
#include <aknviewappui.h>
#include <eikmenub.h>
#include <avkon.hrh>
#include <barsread.h>
#include <stringloader.h>
#include <aknlists.h>
#include <eikenv.h>
#include <akniconarray.h>
#include <eikclbd.h>
#include <akncontext.h>
#include <akntitle.h>
#include <eikbtgpc.h>
#include "SmartRoute.rsg"
#include "SmartRoute.hrh"
#include "MainScreenView.h"
#include "MainScreen.hrh"
#include "TripInfo.hrh"
#include "MainScreen.h"
#include "SmartRouteAppUi.h"

CMainScreenView::CMainScreenView()
	{
	iMainScreen = NULL;
	}

CMainScreenView::~CMainScreenView()
	{
	delete iMainScreen;
	}

CMainScreenView* CMainScreenView::NewL()
	{
	CMainScreenView* self = CMainScreenView::NewLC();
	CleanupStack::Pop( self );
	return self;
	}

CMainScreenView* CMainScreenView::NewLC()
	{
	CMainScreenView* self = new (ELeave) CMainScreenView();
	CleanupStack::PushL( self );
	self->ConstructL();
	return self;
	}

void CMainScreenView::ConstructL()
	{
	BaseConstructL( R_MAIN_SCREEN_MAIN_SCREEN_VIEW );
	}
	
TUid CMainScreenView::Id() const
	{
	return TUid::Uid( EMainScreenViewId );
	}


void CMainScreenView::HandleCommandL( TInt aCommand )
	{   
	CSmartRouteAppUi* appUi = static_cast<CSmartRouteAppUi*>(AppUi());
    CSmartRouteEngine& engine = appUi->EngineL();
    
	switch (aCommand)
		{	
		case EAknCmdExit:
			AppUi()->Exit();
			break;
		case EMainScreenViewHide: 
			{
		    TApaTaskList taskList(CEikonEnv::Static()->WsSession());
		    TApaTask task = taskList.FindByPos(0); //This app must be fg if user has selected cba
		    if(task.Exists())
		        task.SendToBackground();
			}
		    break;
		case ESmartRouteStart:
		case ESmartRouteResume:
			engine.StartL();
			SetCommandsL();
			break;
		case ESmartRouteStop:
			engine.Stop();
			SetCommandsL();
			break;
		case ESmartRoutePause:
			engine.Pause();
			SetCommandsL();
			break;
		case ESmartRouteShow:
			AppUi()->ActivateLocalViewL(TUid::Uid(ERouteListBoxViewId));
			break;
		case ESmartRouteSettings:
			AppUi()->ActivateLocalViewL(TUid::Uid(ESettingsListViewId));
			break;
		default:
		    AppUi()->HandleCommandL(aCommand);
			break;
		}
	}

void CMainScreenView::DoActivateL(
		const TVwsViewId& /*aPrevViewId*/,
		TUid /*aCustomMessageId*/,
		const TDesC8& /*aCustomMessage*/ )
	{
	SetupStatusPaneL();
	
	CEikButtonGroupContainer *cba = AppUi()->Cba();
	if ( cba ) 
		{
		cba->MakeVisible( EFalse );
		}
	
	if ( !iMainScreen )
		{
		iMainScreen = CMainScreen::NewL( ClientRect(), NULL, this );
		iMainScreen->SetMopParent( this );
		AppUi()->AddToStackL( *this, iMainScreen );
		} 
	SetCommandsL();
	}

void CMainScreenView::DoDeactivate()
	{
	CleanupStatusPane();
	
	CEikButtonGroupContainer *cba = AppUi()->Cba();
	if ( cba ) 
		{
		cba->MakeVisible( ETrue );
		cba->DrawDeferred();
		}
	
	if (iMainScreen)
		{
		AppUi()->RemoveFromViewStack( *this, iMainScreen );
		delete iMainScreen;
		iMainScreen = NULL;
		}
	}

void CMainScreenView::SetupStatusPaneL()
	{
	TUid contextPaneUid = TUid::Uid( EEikStatusPaneUidContext );
	CEikStatusPaneBase::TPaneCapabilities subPaneContext = 
		StatusPane()->PaneCapabilities( contextPaneUid );
	if ( subPaneContext.IsPresent() && subPaneContext.IsAppOwned() )
		{
		CAknContextPane* context = static_cast< CAknContextPane* > ( 
			StatusPane()->ControlL( contextPaneUid ) );
		context->SetPictureToDefaultL();
		}

	TUid titlePaneUid = TUid::Uid( EEikStatusPaneUidTitle );
	CEikStatusPaneBase::TPaneCapabilities subPaneTitle = 
		StatusPane()->PaneCapabilities( titlePaneUid );
	if ( subPaneTitle.IsPresent() && subPaneTitle.IsAppOwned() )
		{
		CAknTitlePane* title = static_cast< CAknTitlePane* >( 
			StatusPane()->ControlL( titlePaneUid ) );
		TResourceReader reader;
		iEikonEnv->CreateResourceReaderLC( reader, R_MAIN_SCREEN_TITLE_RESOURCE );
		title->SetFromResourceL( reader );
		CleanupStack::PopAndDestroy();
		}
				
	}

void CMainScreenView::CleanupStatusPane()
	{
	}

void CMainScreenView::HandleStatusPaneSizeChange()
	{
	CAknView::HandleStatusPaneSizeChange();
	TInt result;
	TRAP( result, SetupStatusPaneL() ); 
	}

void CMainScreenView::HandleForegroundEventL(TBool aForeground)
    {
    }
    
void CMainScreenView::SetCommandsL()
    {
    CSmartRouteAppUi* appUi = static_cast<CSmartRouteAppUi*>(AppUi());
    TSmartRouteState state = appUi->EngineL().State();
    
    switch(state)
        {
        case ERouteIdle:
            iMainScreen->SetupStoppedL();
            break;
        case ERouteRecording:
            iMainScreen->SetupRunningL();
            break;
        case ERoutePaused:
            iMainScreen->SetupPausedL();
            break;
        }
    }

				
